package org.xujin.book.test;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.xujin.book.po.Student;
/**
 * 
* @ClassName: QueryAll
* @Description: TODO
* @author Xu,Jin Software_King@qq.com
* @Site:http://www.xujin.org
 */
public class QueryAll {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) { 
		/*创建一个Configuration()的实例，调用configure()方法加载hibernate.cfg.xml配置文件
		 * 如果hibernate.cfg.xml没有放在默认的位置:classpath下
		 * 举例：Configuration conf = new Configuration().configure("/hibernate.cfg.xml配置文件的路径/hibernate.cfg.xml");
		 * 
		 * */
		Configuration conf = new Configuration().configure();
		/*conf调用buildSessionFactory()方法创建SessionFactory,相当于创建数据源*/
		SessionFactory sf = conf.buildSessionFactory();
		/*打开会话*/
		Session s = sf.openSession();
		Transaction tx=s.beginTransaction();
		
		/*
		 * hql:面向对象的语句
		 * Query:接口:执行对数据库的crud操作
		 */
	   Query query=s.createQuery("from Student stu");
	   List<Student> list=query.list();
		for(Student stu:list){
			System.out.println("学号:" + stu.getStuno());
			System.out.println("姓名:" + stu.getName());
			System.out.println("年龄:" + stu.getAge());
			System.out.println("**********************************");
		}
	  
	   tx.commit();
	   //关闭session
		s.close();
	}
}
